//
//  BaseController.m
//  bus
//
//  Created by hychen on 17/3/30.
//  Copyright © 2017年 Hychen. All rights reserved.
//

#import "BaseController.h"
//#import "StatisticTool_lib.h"
@interface BaseController ()
@property (nonatomic, strong) UIButton *backButton;
@end

@implementation BaseController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //友盟页面统计
//    [[StatisticTool_lib sharedTool] beginLogPageWithName:NSStringFromClass([self class])];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //友盟页面统计
//    [[StatisticTool_lib sharedTool] endLogPageWithName:NSStringFromClass([self class])];
    //关闭菊花
    Dismiss_NoMsg
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Color_Background;
    
    [self prepareBackButton];
    
    self.nilDataView = [[UIImageView alloc] initWithImage:ImageNamed_bb(@"nodata")];
    [self.view addSubview:self.nilDataView];
    self.nilDataView.frame = CGRectMake((Screen_Width-90)*0.5, (Screen_Height-64-90)*0.5, 90, 90);
    self.nilDataView.hidden = YES;
    [self.view bringSubviewToFront:self.nilDataView];
}

- (void)prepareBackButton{
    [self.navigationController.navigationItem setHidesBackButton:YES];
    [self.navigationController.navigationBar.backItem setHidesBackButton:YES];
    [self.navigationItem.backBarButtonItem setTitle:@""];
    // 返回按钮
    [self.navigationItem setHidesBackButton:YES animated:NO];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(-15, 0, 40, 40);
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [backBtn setImage:[UIImage imageNamed:@"app_back_n_ic"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"app_back_p_ic"] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backBtnDidClick:) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    self.navigationItem.backBarButtonItem = backItem;
    backItem.title = @"";
    self.backButton = backBtn;
    
    self.navigationController.navigationItem.title = @"";
    self.navigationItem.backBarButtonItem.title = @"";
}

- (void)backBtnDidClick:(UIButton *)btn {
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

// 是否显示后退按钮
- (void)isShowBackBtn:(BOOL)b{
    self.backButton.hidden = !b;
}

// Push
- (void)pushWithVcClassName:(NSString *)vcName isHidesBottomBarWhenPush:(BOOL)b{
    UIViewController *vc = [[NSClassFromString(vcName) alloc] init];
    [self pushwithVc:vc isHidesBottomBar:b];
}

- (void)pushwithVc:(UIViewController *)vc isHidesBottomBar:(BOOL)b{
    vc.hidesBottomBarWhenPushed = b;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)dealloc{
    NSLog(@"控制器:<%@> 释放了",self);
}

- (NSString *)getWeekdayWithTimeString:(NSString *)timeString {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *startDate = [[NSDate alloc]init];
    
    NSString *startString = timeString;
    startString = [startString substringToIndex:16];
    
    startDate = [dateFormatter dateFromString:startString];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    comps = [calendar components:unitFlags fromDate:startDate];
    
    NSLog(@"===%ld",(long)[comps weekday]);
    
    NSString *weekDay;
    switch ([comps weekday]) {
        case 1:
            weekDay = @"星期日";
            break;
        case 2:
            weekDay = @"星期一";
            break;
        case 3:
            weekDay = @"星期二";
            break;
        case 4:
            weekDay = @"星期三";
            break;
        case 5:
            weekDay = @"星期四";
            break;
        case 6:
            weekDay = @"星期五";
            break;
        case 7:
            weekDay = @"星期六";
            break;
            
        default:
            break;
    }
    
    return weekDay;
    
}

- (void)showNoDataView{
    self.nilDataView.hidden = NO;
    [self.view bringSubviewToFront:self.nilDataView];
}

- (void)hideNoDataView{
    self.nilDataView.hidden = YES;
    [self.view sendSubviewToBack:self.nilDataView];
}

@end
